package cn.lcfms.app.admin.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.lcfms.app.admin.bean.AreasBean;
import cn.lcfms.app.admin.service.Dao.CommonDao;
import cn.lcfms.bin.core.Application;
import cn.lcfms.bin.service.BaseService;
import cn.lcfms.utils.HttpUtils;

@Service
public class CommonService {
	private static String username;
	private static String password;
	@Autowired
	private CommonDao cd;
	
	public String[] getMessageBase() {
		String[] r=new String[2];
		if(null!=username || null!=password) {
			r[0]=username;
			r[1]=password;
			return r;
		}
		List<HashMap<String, Object>> list = cd.selectMessageBase();
		for(HashMap<String, Object> map:list) {
			String type=(String) map.get("type");
			String value=(String) map.get("value");
			if(type.equals("username")) {
				username=value;
				r[0]=username;
			}
			if(type.equals("password")) {
				password=value;
				r[1]=password;
			}
		}
		return r;
	}
	
	public void clearMessageBase() {
		username=null;
		password=null;
	}
	/**
	 * 根据AreasBean中的id获取完整地址信息,必须省,市、州的id都有才有结果
	 * @param bean
	 * @return
	 */
	public String getAddress(AreasBean bean){
		BaseService service= Application.getService("areas");
		StringBuffer sql=new StringBuffer();
		if(bean.getS_province()!=0){
			sql.append("select * from areas where id="+bean.getS_province());
		}
		if(bean.getS_city()!=0){
			sql.append(" union select * from areas where id="+bean.getS_city());
		}
		if(bean.getS_county()!=0){
			sql.append(" union select * from areas where id="+bean.getS_county());
		}
		if(bean.getS_street()!=0){
			sql.append(" union select * from areas where id="+bean.getS_street());
		}
		service.sql(sql.toString());
		List<HashMap<String, Object>> list = service.getResult();
		if(list.size()>=1){
			bean.setS_province_str((String)list.get(0).get("name"));
		}
		if(list.size()>=2){
			bean.setS_city_str((String)list.get(1).get("name"));
		}
		if(list.size()>=3){
			bean.setS_county_str((String)list.get(2).get("name"));
		}
		if(list.size()==4){
			bean.setS_street_str((String)list.get(3).get("name"));
		}
		return bean.toString();
	} 
	
	public String getAddress(int id){	
		List<String> list=new ArrayList<String>();
		getAddressById(id,list);
		StringBuffer buffer=new StringBuffer();
		for(int i=list.size()-1;i>=0;i--){
			buffer.append(list.get(i));
			if(i!=0){
				buffer.append("，");
			}
		}
		return buffer.toString();
	}
	
	public void sendMsg(String content,String mobile) throws Exception{		
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("account", getMessageBase()[0]);
		map.put("password", getMessageBase()[1]);
		//map.put("account", "C67653871");
		//map.put("password", "63fc9ea64c6530a396a13783087a2dee");
		map.put("mobile", mobile);
		map.put("content", content);
		String r = HttpUtils.postData(map, "http://106.ihuyi.cn/webservice/sms.php?method=Submit");
		Document document = DocumentHelper.parseText(r); 
		Element element=document.getRootElement();
		Element code = element.element("code");
		if(!code.getText().equals("2")) {
			Element msg = element.element("msg");
			throw new Exception(msg.getText());
		}
	}
	
	private void getAddressById(int id,List<String> buffer){
		HashMap<String, Object> map = cd.selectAddressById(id);
		if(map==null){
			return;
		}
		int parent_id = (int) map.get("parent_id");
		if(parent_id!=0){
			buffer.add((String) map.get("name"));
			getAddressById(parent_id,buffer);
		}else{
			buffer.add((String) map.get("name"));
		}		
	}
	
	public String parseListToString(String col,List<HashMap<String, Object>> list) {
		StringBuilder builder=new StringBuilder();
		int i=0;
		for(HashMap<String, Object> map:list) {
			builder.append(map.get(col));
			if(i<list.size()-1) {
				builder.append(",");
			}
			i++;
		}
		return builder.toString();
	}
	
	public void testAop() {
		System.out.println("测试AOP");
	}
}
